import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/generated/l10n.dart';
import '../../config/app_color.dart';

/// 项目中通用确认按钮
class CommonButton extends StatefulWidget {
  // const CommonButton({super.key});
  final String? title;
  final Function()? onClick;
  final Future Function()? onAsyncClick;
  final double? height;
  final double? width;
  final double? fontSize;
  final FontWeight? fontWeight;
  final Color? textColor;
  final bool enable;
  final bool alwaysCanClick;
  final Duration? delayed;
  final bool isLine;

  // 最小宽度
  const CommonButton({
    super.key,
    this.title,
    this.onClick,
    this.enable = true,
    this.height,
    this.width,
    this.fontSize,
    this.fontWeight = FontWeight.w500,
    this.textColor,
    this.alwaysCanClick = false,
    this.onAsyncClick,
    this.isLine = false,
    this.delayed = const Duration(milliseconds: 250),
  });

  // 最大宽度
  const CommonButton.expend({
    super.key,
     this.title,
    this.onClick,
    this.enable = true,
    this.height,
    this.width = double.infinity,
    this.fontSize,
    this.isLine = false,
    this.fontWeight = FontWeight.w500,
    this.textColor,
    this.alwaysCanClick = false,
    this.onAsyncClick,
    this.delayed = const Duration(milliseconds: 250),
  });

  @override
  State<CommonButton> createState() => _CommonButtonState();
}

class _CommonButtonState extends State<CommonButton> {
  bool _isTapped = false;
  bool _isLoading = false;

  @override
  Widget build(BuildContext context) {
    return Opacity(
      opacity: widget.enable ? 1 : 0.5,
      child: GestureDetector(
          onTap: widget.alwaysCanClick || widget.enable ? _onTap : () {},
          child: Container(
            height: widget.height ?? 44,
            width: widget.width,
            decoration:widget.isLine ? BoxDecoration(
                borderRadius: BorderRadius.circular(5),
              border: Border.all(
                width: 1,
                color: ThemeColor.colorTextB2
              )

            ) :  BoxDecoration(
                gradient: const LinearGradient(colors: [
                  ThemeColor.colorBtnL1,
                  ThemeColor.colorBtnL2,
                  ThemeColor.colorBtnL3,
                ], begin: Alignment.centerLeft, end: Alignment.centerRight),
                borderRadius: BorderRadius.circular(5)),
            alignment: Alignment.center,
            child: _isLoading
                ? const SizedBox(
                    height: 20,
                    width: 20,
                    child: CupertinoActivityIndicator(
                      radius: 10,
                      color: ThemeColor.colorTextB0,
                    ))
                : Text(
                    widget.title ?? S().sure,
                    style: TextStyle(
                        fontSize: widget.fontSize ?? 16.sp,
                        fontWeight: widget.fontWeight ?? FontWeight.w500,
                        color: widget.textColor ?? ThemeColor.colorTextB2),
                  ),
          )),
    );
  }

  _onTap() {
    FocusManager.instance.primaryFocus?.unfocus();
    if (!_isTapped) {
      _isTapped = true;
      if (widget.onClick != null) {
        _tapWithOnTap();
      } else if (widget.onAsyncClick != null) {
        _tapWithAsyncTap();
      }
    }
    // else {
    //   if (!LoadingUtils.isShow) {
    //     LoadingUtils.showToast(LocaleKeys.toast_debounce_operation.tr);
    //   }
    // }
  }

  void _tapWithAsyncTap() async {
    if (mounted) {
      setState(() {
        _isLoading = true;
      });
    }
    await widget.onAsyncClick?.call();
    _isTapped = false;
    if (mounted) {
      setState(() {
        _isLoading = false;
      });
    }
    // sendEventClick(name: statisticsAlias);
  }

  void _tapWithOnTap() {
    widget.onClick?.call();
    if (widget.delayed == null) {
      _isTapped = false;
    } else {
      Future.delayed(widget.delayed!, () {
        _isTapped = false;
      });
    }
    // sendEventClick(name: statisticsAlias);
  }
}
